<?php
class Admin extends Controller {
	function Admin() {
		parent::Controller();
		
		$this->load->library('common');
		$this->load->library('cache');
	}
	
	function _check_admin() {
		$is_admin = $this->common->GetSession('is_admin');
		if(!$is_admin) {
			redirect('admin/login');
		}
	}
	
	function _do_login($pwd) {
		$admin_pwd = $this->config->item('AdminPwd');
		if(strcmp($admin_pwd, $pwd) == 0) {
			$this->common->SetSession('is_admin', 'ja');
			redirect('admin');
		} else {
			die('Password wrong!');
		}
	}
	
	function index() {
		$this->_check_admin();
		$this->load->view('admin/home_view');
	}
	
	function login() {
		if($this->input->post('hidIsPostBack')) {
			$pass = $this->input->post('txtAdminPwd');
			$this->_do_login($pass);
		} else {
			$this->load->view('admin/login_view');
		}
	}
	
	function users() {
		$this->_check_admin();
		
		$total = $this->db->query('SELECT COUNT(`id`) AS num FROM `nt_bloggers`');
		$total_num = $total->row()->num;
		$showing = $this->db->query('SELECT COUNT(`blogger_id`) AS `num` FROM `nt_blogger_info` WHERE `show` = 1');
		$showing_num = $showing->row()->num;
		$hiding = $this->db->query('SELECT COUNT(`blogger_id`) AS `num` FROM `nt_blogger_info` WHERE `show` = 0');
		$hiding_num = $hiding->row()->num;
		$vdata['total_num'] = $total_num;
		$vdata['showing_num'] = $showing_num;
		$vdata['hiding_num'] = $hiding_num;
		
		$users = $this->db->query('SELECT b.`id`, b.`user_name`, b.`user_url`, bi.`title`, bi.`show`, bi.`last_update_time` FROM `nt_blogger_info` bi JOIN `nt_bloggers` b ON bi.`blogger_id` = b.`id` ORDER BY b.`id`');
		$vdata['users'] = $users->result();
		
		$this->load->view('admin/users_view', $vdata);
	}
	
	function reset_pwd($blogger_id) {
		$this->_check_admin();
		
		$default_pwd = 'letmein';
		
		$this->db->query('UPDATE `nt_bloggers` SET `password` = ? WHERE `id` = ?', array($default_pwd, $blogger_id));
		if($this->db->affected_rows() == 1) {
			echo("@ID $blogger_id Reset-Pwd: 200 OK");
		} else {
			echo("@ID $blogger_id Reset-Pwd: 500 NOK");
		}
	}
	
	function show_hide($blogger_id, $old_state) {
		$new_state = 0;
		if($old_state == 0) {
			$new_state = 1;
		}
		
		$this->db->query('UPDATE `nt_blogger_info` SET `show` = ? WHERE `blogger_id` = ?', array($new_state, $blogger_id));
		
		// clear cache
		$this->cache->Remove('blogger_list');
		$this->cache->Remove('latest_entries');
		redirect('admin/users');
	}
	
	function set_user_url($blogger_id) {
		$error_msg = '';
		if($this->input->post('hidIsPostBack')) {
			$url = $this->input->post('txtUrl');
			$test = $this->db->query('SELECT * FROM `nt_bloggers` WHERE `user_url` = ?', array($url));
			if($test->num_rows() == 0) {
				// no duplication
				$this->db->query('UPDATE `nt_bloggers` SET `user_url` = ? WHERE `id` = ?', array($url, $blogger_id));
				// clear cache
				$this->cache->Remove('blogger_list');
				$this->cache->Remove('latest_entries');
				redirect('admin/users');
			} else {
				$error_msg = 'URL already used!';
			}
		}
		$uname = $this->common->GetUserNameByID($blogger_id);
		$vd['blogger_name'] = $uname;
		$vd['error_msg'] = $error_msg;
		$vd['blogger_id'] = $blogger_id;
		$this->load->view('admin/set_user_url_view', $vd);
	}
	
	function delete_blogger($blogger_id) {
		// ...
	}
	
	function register() {
		$this->_check_admin();
		
		$has_error = FALSE;
		$error_msg = '';
		$new_blogger_info = array(
			'id' => 0,
			'user_name' => '',
			'password' => '',
			'url' => '',
			'blog_title' => ''
		);
		$show_reg_info = FALSE;
		
		$ipb = $this->input->post('hidIsPostBack');
		if($ipb == 'ja') {
			$user_name = $this->input->post('txtUserName');
			$password = $this->input->post('txtPassword');
			$pwd_rep = $this->input->post('txtPwdRep');
			$email = $this->input->post('txtEMail');
			$url = $this->input->post('txtUrl');
			$blog_title = $this->input->post('txtBlogTitle');
			
			if($user_name == '' || $password == '' || $pwd_rep == '' || $email == '' || $url == '' || $blog_title == '') {
				$has_error = TRUE;
				$error_msg = 'All the fields cannot be empty!';
			} elseif(strcmp($password, $pwd_rep) != 0) {
				$has_error = TRUE;
				$error_msg = 'Password and repeat don\'t match!';
			} else {
				$this->load->model('Blogger_Manager');
				$reg_info = array(
					'user_name' => $user_name,
					'password' => $password,
					'email' => $email,
					'user_url' => $url,
					'title' => $blog_title,
					'reg_date' => $this->common->Now(),
					'reg_ip' => $this->input->ip_address()
				);
				$new_blogger_id = $this->Blogger_Manager->Register($reg_info);
				if($new_blogger_id == 0) {
					$has_error = TRUE;
					$error_msg = 'User-Name or URL has already been registered, try again!';
				} else {
					$new_blogger_info['id'] = $new_blogger_id;
					$new_blogger_info['user_name'] = $user_name;
					$new_blogger_info['password'] = $password;
					$new_blogger_info['url'] = $url;
					$new_blogger_info['blog_title'] = $blog_title;
					$show_reg_info = TRUE;
					
					// clear cache
					$this->cache->Remove('blogger_list');
				}
			}
		}
		$vd['has_error'] = $has_error;
		$vd['error_msg'] = $error_msg;
		$vd['new_blogger_info'] = $new_blogger_info;
		$vd['show_reg_info'] = $show_reg_info;
		
		$this->load->view('admin/register_view', $vd);
	}
	
	function announcement() {
		$ann_file_path = $_SERVER['DOCUMENT_ROOT'] . '/data/home_ann.txt';
		if($this->input->post('hidIsPostBack')) {
			$ann = $this->input->post('txtAnn');
			file_put_contents($ann_file_path, $ann);
			redirect('admin/announcement');
		} else {
			$ann1 = file_get_contents($ann_file_path);
			$vd['ann'] = $ann1;
			$this->load->view('admin/announcement_view', $vd);
		}
	}
	
	function entries() {
		echo('TODO');
	}
	
}
?>